let app = getApp()
Page({
  data: {
    isLogin: app.globalData.isLogin,
    userInfo: app.globalData.userInfo,
    superM: false
  },
  onLoad() {
    this.setData({
      isLogin: app.globalData.isLogin,
      userInfo: app.globalData.userInfo,
      nickName: this.data.userInfo && this.data.userInfo.nickName
    })
  },
  onShow() {
    if (this.data.isLogin) {
      wx.cloud.callFunction({
        name: "db",
        data: {
          $url: "getUserInfo"
        }
      }).then(res => {
        if (res.result.code) {
          if (res.result.userInfo.OPENID === "obkvc5dVO8MwPE5jk1dPcJXOqdFQ") {
            // wx.showToast({
            //   title: "超级管理员模式已开启！",
            //   icon: 'none',
            //   duration: 2000
            // })
            console.log("超级管理员模式已开启！");
            this.setData({
              superM: true
            })
          }
          app.$db.set('userInfo', res.result.userInfo)
          this.setData({
            userInfo: res.result.userInfo,
            nickName: res.result.userInfo.nickName
          })
          app.globalData.userInfo = res.result.userInfo
        }
      })
    }
  },
  wxGetUserInfo() {
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (e) => {
        if (e.userInfo) {
          var that = this;
          let userInfo = e.userInfo
          wx.showLoading({
            title: '登录中',
          })
          wx.cloud.callFunction({
            name: "db",
            data: {
              $url: "login",
              userInfo: {
                nickName: userInfo.nickName,
                gender: userInfo.gender,
                avatarUrl: userInfo.avatarUrl
              }
            }
          }).then(res => {
            if (res.result.code) {
              app.$db.set('userInfo', res.result.userInfo)
              this.setData({
                userInfo: res.result.userInfo,
                nickName: res.result.userInfo.nickName
              })
              app.globalData.userInfo = res.result.userInfo
              app.globalData.isLogin = true
              wx.hideLoading({})
              wx.showToast({
                title: '登录成功',
                icon: 'success',
                duration: 1500
              })
              app.$util.reLaunch("/pages/index/index")
              that.setData({
                isLogin: true
              });
            }
          })
        } else {
          wx.showModal({
            title: '警告',
            content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
            showCancel: false,
            confirmText: '返回授权',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击了“返回授权”');
              }
            }
          });
        }
      },
      fail(e) {
        console.log(e)
      }
    })
  },
  goLikeList() {
    wx.navigateTo({
      url: '/pages/likeList/likeList',
    })
  },
  goWrongList() {
    wx.navigateTo({
      url: '/pages/wrongList/wrongList',
    })
  },
  goIntegral() {
    wx.navigateTo({
      url: '/pages/integral/integral',
    })
  },
  goAdmin() {
    wx.navigateTo({
      url: '/pages/adminList/adminList',
      // url: '/pages/admin/admin',
    })
  },
})